

import os
import pandas as pd
# 分类表
def group_by_prefix(lst):
    grouped = {}
    for item in lst:
        # 获取前六个字符作为键
        prefix = item[:6]
        # 将元素添加到对应的组中
        if prefix not in grouped:
            grouped[prefix] = []
        grouped[prefix].append(item)

    return list(grouped.values())


filedir = r'D:\学习&科研\华为手表项目\华为数据\subject'
csvdir=r'D:\学习&科研\华为手表项目\华为数据统计和分析'
data1={
    'id':[],
    'ppg':[],
    'ecg':[],
    'rrdata':[],
    'singlework':[],
    'singledetail':[],
    'rri':[]
}

data2={
'id':[],
    'ppg':[],
    'ecg':[],
    'rrdata':[],
    'singlework':[],
    'singledetail':[],
    'rri':[],
    'img':[]
}
data3={
'id':[],
    'ecg': [],
    'rrdata': [],
    'fit':[],
    'img':[]
}
data4={
'id':[],
    'ecg': [],
    'rrdata': [],
    'fit': [],
    'img': []
}
data1 = pd.DataFrame(data1)
data2 = pd.DataFrame(data2)
data3 = pd.DataFrame(data3)
data4 = pd.DataFrame(data4)
# 列出目录中的所有文件和文件夹
files = os.listdir(filedir)
print("目录中的文件和文件夹:")
for file in files:

    files = os.listdir(filedir+'\\'+file)
    # print(files)
    group_list=group_by_prefix(files)
    print(group_list)
    for group in group_list:
        if group[0][5]=='1':
            ppgcount = len([item for item in group if 'ppg' in item])
            ecgcount=len([item for item in group if 'ecg' in item])
            rrdatacount=len([item for item in group if 'rrdata' in item])
            singleworkcount=len([item for item in group if 'singlework' in item])
            singledetailcount=len([item for item in group if 'singledetail' in item])
            rricount=len([item for item in group if 'rri' in item])
            new_row = {'id':group[0][0:8],'ppg':ppgcount,'ecg':ecgcount,'rrdata':singleworkcount,'singlework':singleworkcount,'singledetail':singledetailcount,'rri':rricount}
            data1.loc[len(data1)] = new_row
            data1 = data1[['id', 'ppg', 'ecg','rrdata','singlework','singledetail','rri']]


        if group[0][5]=='2':
            ppgcount = len([item for item in group if 'ppg' in item])
            ecgcount = len([item for item in group if 'ecg' in item])
            rrdatacount = len([item for item in group if 'rrdata' in item])
            singleworkcount = len([item for item in group if 'singlework' in item])
            singledetailcount = len([item for item in group if 'singledetail' in item])
            rricount = len([item for item in group if 'rri' in item])
            imgcount = len([item for item in group if 'img' in item])
            new_row = {'id':group[0][0:8],'ppg':ppgcount,'ecg':ecgcount,'rrdata':singleworkcount,'singlework':singleworkcount,'singledetail':singledetailcount,'rri':rricount,'img':imgcount}
            data2.loc[len(data2)] = new_row
            data2 = data2[['id', 'ppg', 'ecg', 'rrdata', 'singlework', 'singledetail', 'rri','img']]

        if group[0][5] == '3':
            ecgcount = len([item for item in group if 'ecg' in item])
            rrdatacount = len([item for item in group if 'rrdata' in item])
            imgcount = len([item for item in group if 'img' in item])
            fitcount = len([item for item in group if 'fit' in item])
            new_row = { 'id':group[0][0:8],'ecg':ecgcount,'rrdata': rrdatacount,'fit':fitcount,'img':imgcount}
            data3.loc[len(data3)] = new_row
            data3=data3[['id',  'ecg', 'rrdata','fit','img']]
        if group[0][5] == '4':
            ecgcount = len([item for item in group if 'ecg' in item])
            rrdatacount = len([item for item in group if 'rrdata' in item])
            imgcount = len([item for item in group if 'img' in item])
            fitcount = len([item for item in group if 'fit' in item])
            new_row = {'id': group[0][0:8], 'ecg': ecgcount, 'rrdata': rrdatacount, 'fit': fitcount, 'img': imgcount}
            data4.loc[len(data4)] = new_row
            data4 = data4[['id', 'ecg', 'rrdata', 'fit', 'img']]

print(data1,data2,data3,data4)

data1.to_csv(csvdir+'\\'+'华为室内.csv', index=False,)
data2.to_csv(csvdir+'\\'+'华为室外.csv', index=False,)
data3.to_csv(csvdir+'\\'+'佳明.csv', index=False,)
data4.to_csv(csvdir+'\\'+'高驰.csv', index=False,)